package com.ruoyi.client.utils;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class HttpClientUtils {

    //传入经纬度, 返回查询的地区, lng: 纬度, lat: 经度
    public static Map<String, Object> findByLatAndLng(String lng, String lat) {
        try {

            CloseableHttpClient httpClient = HttpClients.createDefault();
            // url中的ak值要替换成自己的:   AS9yto20ojxcozxB4syybQK1p8gnFGbp
            String url = "http://api.map.baidu.com/reverse_geocoding/v3/?ak=RAFP4WWoO1f7XHznlZjibhzUBxGfYYGT&output=json&coordtype=wgs84ll&location=" + lng + "," + lat;
            HttpGet httpGet = new HttpGet(url);

            CloseableHttpResponse response = httpClient.execute(httpGet);

            HttpEntity httpEntity = response.getEntity();

            String json = EntityUtils.toString(httpEntity);

            Map<String, Object> result = JSONObject.parseObject(json, Map.class);

            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


//    public static void main(String[] args) {
//        //124.802554,51.132573  前边是纬度,  后边经度.
//        //49.146493,126.568695
//        Map<String, Object> byLatAndLng = HttpClientUtils.findByLatAndLng("23.172618", "113.411493");
//        Map<String, Object> cityMap = new HashMap<>();
//        if (byLatAndLng.get("status").equals(0)) {
//            Map<String, Object> resultMap = (Map<String, Object>) byLatAndLng.get("result");
//            resultMap = (Map<String, Object>) resultMap.get("addressComponent");
//            cityMap.put("country", (String) resultMap.get("country"));
//            cityMap.put("province", (String) resultMap.get("province"));
//            cityMap.put("city", (String) resultMap.get("city"));
//        }
//        System.out.println("地址是: " + cityMap);
//    }

//    public static void main(String[] args) {
//
//        try {
//            CloseableHttpClient httpClient = HttpClients.createDefault();
//
//            String httpUrl = "http://api.map.baidu.com/geocoding/v3/?output=json&location=showLocation&ak=RAFP4WWoO1f7XHznlZjibhzUBxGfYYGT&address=" + "广东省广州市白云区蟾蜍石北街12号";
//            // 创建默认http连接
//            HttpGet httpGet = new HttpGet(httpUrl);
//
//            CloseableHttpResponse response = httpClient.execute(httpGet);
//
//            HttpEntity httpEntity = response.getEntity();
//
//            String json = EntityUtils.toString(httpEntity);
//
//            Map<String, Object> result = JSONObject.parseObject(json, Map.class);
//
//            System.out.println("result = " + result);
//
//        }catch (Exception e){
//            System.out.println("e = " + e);
//        }
//    }

}